import java.sql.*;

public class TransactionTest {
    static Connection conn = null;
    static Statement st = null;
    static PreparedStatement ps = null;
    static ResultSet rs= null;
    public static void main(String[] args) throws SQLException{
        money();
        System.out.println("finish");
    }
    public static void money() throws SQLException{
        float m1 = 13;
        try {
            conn = DBConnection.getConnection();
            conn.setAutoCommit(false);

            String sql_1 = "update testDB.student set money=money-? where id=?";
            ps = conn.prepareStatement(sql_1);
            ps.setFloat(1,m1);
            ps.setInt(2,2);
            ps.executeUpdate();

            String sql_2 = "update testDB.student set money=money+? where id=?";
            ps = conn.prepareStatement(sql_2);
            ps.setFloat(1,m1);
            ps.setInt(2,3);
            ps.executeUpdate();
            conn.commit();

        } catch (SQLException e) {
            e.printStackTrace();
            conn.rollback();
        } finally {
            DBConnection.close(rs,ps,conn);
        }
    }
}